package com.parking.mgr.common.context.constant;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.log.Log;
import com.parking.mgr.common.pojo.cryptogram.CryptogramConfigs;

import java.nio.charset.StandardCharsets;

/**
 * @ClassName ConstantContextHolder
 * @Description 系统参数配置获取
 * @Auther: zhijianpu
 * @Date: 2023/5/23
 */
public class ConstantContextHolder {

    private static final Log log = Log.get();

    public static Boolean getCaptchaOpenFlag() {
        return getSysConfigWithDefault("CAPTCHA_OPEN", Boolean.class, true);
    }

    /**
     * 获取token过期时间（单位：秒）
     * 默认时间1天
     *
     * @return
     */
    public static Long getTokenExpireSec() {
        return getSysConfigWithDefault("TOKEN_EXPIRE", Long.class, 7 * 86400L);
    }

    /**
     * 获取jwt密钥，默认是32位随机字符串
     *
     * @return
     */
    public static byte[] getJwtSecret() {
        return getSysConfigWithDefault("JWT_SECRET", String.class, Base64.encode(RandomUtil.randomString(32))).getBytes(StandardCharsets.UTF_8);
    }

    /**
     * 获取加解密的配置
     *
     * @return
     */
    public static CryptogramConfigs getCryptogramConfigs() {
        boolean tokenEncDec = getSysConfigWithDefault("TOKEN_ENCRYPTION_OPEN", Boolean.class, true);
        boolean visLogEnc = getSysConfigWithDefault("VISLOG_ENCRYPTION_OPEN", Boolean.class, true);
        boolean opLogEnc = getSysConfigWithDefault("OPLOG_ENCRYPTION_OPEN", Boolean.class, true);
        boolean fieldEncDec = getSysConfigWithDefault("FIELD_ENC_DEC_OPEN", Boolean.class, true);

        CryptogramConfigs cryptogramConfigs = new CryptogramConfigs();
        cryptogramConfigs.setTokenEncDec(tokenEncDec);
        cryptogramConfigs.setVisLogEnc(visLogEnc);
        cryptogramConfigs.setOpLogEnc(opLogEnc);
        cryptogramConfigs.setFieldEncDec(fieldEncDec);
        return cryptogramConfigs;
    }

    /**
     * 获取config表中的配置，如果为空返回默认值
     *
     * @param configCode    变量名称，对应sys_config表中的code
     * @param clazz         返回变量值的类型
     * @param defaultValue  如果结果为空返回此默认值
     * @return
     * @param <T>
     */
    public static <T> T getSysConfigWithDefault(String configCode, Class<T> clazz, T defaultValue) {
        String configValue = ConstantContext.me().getStr(configCode);
        if (ObjectUtil.isEmpty(configValue)) {
            // 将默认值加入到缓存常量
            log.warn(">>> 系统配置sys_config表中存在空项，configCode为：{}，系统采用默认值：{}", configCode, defaultValue);
            ConstantContext.me().put(configCode, defaultValue);
            return defaultValue;
        } else {
            try {
                return Convert.convert(clazz, configValue);
            } catch (Exception e) {
                return defaultValue;
            }
        }
    }

    /**
     * 获取自定义的windows环境本地文件上传路径
     *
     */
    public static String getDefaultFileUploadPathForWindows() {
        return getSysConfigWithDefault("FILE_UPLOAD_PATH_FOR_WINDOWS", String.class, "");
    }

    /**
     * 获取自定义的linux环境本地文件上传路径
     *
     */
    public static String getDefaultFileUploadPathForLinux() {
        return getSysConfigWithDefault("FILE_UPLOAD_PATH_FOR_LINUX", String.class, "");
    }
}
